package mms.farina.thecell.support;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import android.content.Context;

/**Classe per la gestione dell'IO*/
public class IO {

	public IO() {

	}
	
	public static void writeOnMemory(Context context, String filename, byte[] data){
		
		String FILENAME = filename;

		FileOutputStream fos;
		try {
			fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
			fos.write(data);
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	public static byte[] readFromMemory(Context context, String filename) throws IOException{
		
		String FILENAME = filename;
		FileInputStream fis;
		fis=context.openFileInput(FILENAME);
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while (fis.available()>0) {
		   bos.write(fis.read());
		}
		byte[] bytes = bos.toByteArray();
		
		return bytes;
	}
	
	public static void deleteFileFromMemory(Context context, String filename){
		
		context.deleteFile(filename);
		
	}

}
